I am using delegates in program. But situation is to more than one method to adding to a delegate. Please anyone can give me a solution with example how to done this?
Multicast delegate is an extension of normal delegate. It helps you to point more than one method at a single moment of time.
In a windows application, we created a button on a form and on the button click, we want to invoke two methods (AlertMsg(), WarningMsg()). So the code is as follow:
public class Deligatedemo { public Deligatedemo () { } // Declare a delegate publicdelegatevoidNotification(stringmsg); public void Process(Notification handler) { if (handler != null) handler("Alert Message"); if (handler != null) handler("Warning Message"); } } public class demoClass { //This method will show alert message to user public static void AlertMsg (string s) { MessageBox.Show(s); }
//This method will show warning message to user public static void WarningMsg(string s) { MessageBox.Show(s); } } private void button1_Click(object sender, System.EventArgs e) { Deligatedemo dc =newDeligatedemo(); Deligatedemo. Notification notified =null; notified+=newDeligateClass. Notification (AlertMsg); notified+=newDeligateClass. Notification (WarningMsg); dc.Process(notified); }
Liked By
Write Answer
Use of Multicast Delegate in C#?
Join MindStick Community
You have need login or register for voting of answers or question.
Anupam Mishra
28-Jan-2016Multicast delegate is an extension of normal delegate. It helps you to point more than one method at a single moment of time.
In a windows application, we created a button on a form and on the button click, we want to invoke two methods (AlertMsg(), WarningMsg()). So the code is as follow: